package com.quanta.edaily.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

import static java.lang.Math.abs;
import static java.lang.Math.pow;

public class RanUtils {

    public static String genNumStr(int i) { // 最高18*2位
        Random ran = new Random();
        long l1 = (long) (abs(ran.nextLong())%(pow(10,i/2-1)*9)+pow(10,i/2-1));
        long l2 = (long) (abs(ran.nextLong())%(pow(10,i-i/2)));
//        long n1 = ran.nextLong() % (long) Math.pow(10,i/2);
//        long n2 = ran.nextLong() % (long) Math.pow(10,i-i/2);
//        String s1  = String.format("%0"+i/2+"d",l1);
        String s1 = String.valueOf(l1);
        String s2  = String.format("%0"+(i-i/2)+"d",l2);
        return s1+s2;
    }

    public static String genUUID() {
            return UUID.randomUUID().toString().replace("-","");
    }

    public static String genToken(Integer id, String password) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR,24); // 指定令牌过期时间
       return JWT.create()
               .withAudience(id.toString()) // payload
               .withExpiresAt(instance.getTime())
               .sign(Algorithm.HMAC256(password)); // 签名
    }
}
